package com.lazyfish.superdir.service;

import com.lazyfish.superdir.dao.DirDao;
import com.lazyfish.superdir.entity.Dir;
import com.lazyfish.superdir.entity.FatherDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;
@Service
public class DirServiceImpl implements DirService{
    @Autowired
    private DirDao dirDao ;
    public int getTotal() {
        return dirDao.getTotal();
    }

    public Dir getDir(int id) {
        return dirDao.getDir(id);
    }

    public List<Dir> getDirList() {
        return dirDao.getDirList();
    }

    public int makeDir(Dir dir) {
        return dirDao.makeDir(dir);
    }

    @Override
    public int updateDirCollection(int collect,int id) {
        return  dirDao.updateDirCollection(collect,id);
    }

    @Override
    public int deleteDir(int id) {
        return dirDao.deleteDir(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modifyDir(Dir dir) {
        Dir old_dir = dirDao.getDir(dir.getId());
        //获取路径
        try {
            File fdir = new File(FatherDir.getPath()+"/"+old_dir.getName());
            fdir.renameTo(new File(FatherDir.getPath()+"/"+dir.getName()));
        }catch (Exception e){
            throw new RuntimeException();
        }

        return dirDao.modifyDir(dir);
    }
}
